package com.hxd.otc.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.StringRes;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.flyco.dialog.widget.base.BottomBaseDialog;
import com.hxd.otc.R;
import com.hxd.otc.widget.BlocksToast;
import com.hxd.otc.widget.LimitNumberTextWatcher;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lzy on 2018/6/9 0009.
 * description ${提额降额Dialog}
 */
public class CashDepositOperatorDialog extends BottomBaseDialog<CashDepositOperatorDialog> {
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_message)
    TextView mTvMessage;
    @BindView(R.id.tv_money_label)
    TextView mTvMoneyLabel;
    @BindView(R.id.et_money)
    EditText mEtMoney;
    @BindView(R.id.tv_money_unit)
    TextView mTvMoneyUnit;
    @BindView(R.id.tv_require)
    TextView mTvRequire;
    @BindView(R.id.view_hor_line)
    View viewHorLine;
    @BindView(R.id.view_ver_line)
    View viewVerLine;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R.id.tv_present_limit)
    TextView mTvPresentLimit;

    private CharSequence title;
    private CharSequence message;
    private CharSequence moneyLael;
    private CharSequence editTextHint;
    private CharSequence require;
    private CharSequence moneyUnit;
    private CharSequence mEmptyContentWarning;

    private OnConfirmClickListener onConfirmClickListener;
    private CharSequence confirmText;
    private int mEtMoneyScale;

    public CashDepositOperatorDialog(Context context) {
        super(context);
    }

    @Override
    public View onCreateView() {
        return View.inflate(getContext(), R.layout.dialog_cash_deposit_operator, null);
    }

    @Override
    public void onViewCreated(View view) {
        ButterKnife.bind(this, view);
    }

    @Override
    public void setUiBeforShow() {
        mTvTitle.setText(title);
        mTvPresentLimit.setText(message);
        mTvMoneyLabel.setText(moneyLael);
        mEtMoney.setHint(editTextHint);
        mTvRequire.setText(require);
        mTvMoneyUnit.setText(moneyUnit);
        if (!TextUtils.isEmpty(confirmText)) {
            mTvConfirm.setText(confirmText);
        }
        if (mEtMoneyScale > 0) {
            mEtMoney.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
            mEtMoney.addTextChangedListener(new LimitNumberTextWatcher(mEtMoney, 9, 4));
        } else {
            mEtMoney.setInputType(InputType.TYPE_CLASS_NUMBER);
        }
    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                onConfirm();
                break;
        }
    }

    private void onConfirm() {
        String money = mEtMoney.getText().toString().trim();
        if (TextUtils.isEmpty(money) && !TextUtils.isEmpty(mEmptyContentWarning)) {
            BlocksToast.get().showToast(mEmptyContentWarning.toString());
            return;
        }
        if (onConfirmClickListener == null) return;
        onConfirmClickListener.onConfirm(this, money);
    }

    /**
     * 设置确认回调监听
     */
    public CashDepositOperatorDialog setOnConfirmClickListener(CashDepositOperatorDialog.OnConfirmClickListener onConfirmClickListener) {
        this.onConfirmClickListener = onConfirmClickListener;
        return this;
    }

    public interface OnConfirmClickListener {
        /**
         * 根据实现方式决定关闭对话框时机
         *
         * @param dialog   用于关闭对话框等操作
         * @param password 输入框的密码
         */
        void onConfirm(Dialog dialog, String password);
    }


    /**
     * 设置标题
     */
    public CashDepositOperatorDialog setTitleText(CharSequence title) {
        this.title = title;
        return this;
    }

    /**
     * 设置标题
     */
    public CashDepositOperatorDialog setTitleText(@StringRes int resid) {
        setTitleText(getString(resid));
        return this;
    }

    public CashDepositOperatorDialog setMoney(double money) {
        message = String.format(getContext().getString(R.string.my_money_format_msc), money);
        return this;
    }


    /**
     * 设置输入框的提示文本
     */
    public CashDepositOperatorDialog setEditTextHint(CharSequence editTextHint) {
        this.editTextHint = editTextHint;
        return this;
    }

    /**
     * 设置输入框的提示文本
     */
    public CashDepositOperatorDialog setEditHint(@StringRes int resid) {
        setEditTextHint(getString(resid));
        return this;
    }

    public CashDepositOperatorDialog setEditHint(String str) {
        setEditTextHint(str);
        return this;
    }

    /**
     * 设置同意协议内容
     */
    public CashDepositOperatorDialog setRequire(CharSequence require) {
        this.require = require;
        return this;
    }

    /**
     * 设置同意协议内容
     */
    public CashDepositOperatorDialog setRequire(@StringRes int resid) {
        setRequire(getString(resid));
        return this;
    }

    /**
     * 设置金额前置标签
     */
    public CashDepositOperatorDialog setMoneyLabel(CharSequence moneyLael) {
        this.moneyLael = moneyLael;
        return this;
    }

    /**
     * 设置金额前置标签
     */
    public CashDepositOperatorDialog setMoneyLabel(@StringRes int resid) {
        setMoneyLabel(getString(resid));
        return this;
    }

    /**
     * 设置金额单位
     */
    public CashDepositOperatorDialog setMoneyUnit(CharSequence moneyUnit) {
        this.moneyUnit = moneyUnit;
        return this;
    }

    /**
     * 设置金额单位
     */
    public CashDepositOperatorDialog setMoneyUnit(@StringRes int resid) {
        setMoneyUnit(getString(resid));
        return this;
    }

    /**
     * 设置金额单位
     */
    public CashDepositOperatorDialog setConfirmText(CharSequence confirmText) {
        this.confirmText = confirmText;
        return this;
    }

    /**
     * 设置金额单位
     */
    public CashDepositOperatorDialog setConfirmText(@StringRes int resid) {
        setConfirmText(getString(resid));
        return this;
    }

    /**
     * 设置点击确定是，输入框内容为空的提示文本，当不设置时，不进行任何操作
     */
    public CashDepositOperatorDialog setEmptyContentWarning(CharSequence emptyContentWarning) {
        mEmptyContentWarning = emptyContentWarning;
        return this;
    }

    /**
     * 设置点击确定是，输入框内容为空的提示文本，当不设置时，不进行任何操作
     */
    public CashDepositOperatorDialog setEmptyContentWarning(@StringRes int resid) {
        setEmptyContentWarning(getString(resid));
        return this;
    }

    /**
     * 设置编辑框输入小数位
     *
     * @param scale
     * @return
     */
    public CashDepositOperatorDialog setEtMoneyScale(int scale) {
        mEtMoneyScale = scale;
        return this;
    }


    private CharSequence getString(@StringRes int resid) {
        return getContext().getString(resid);
    }
}
